package com.ruoyi.web.controller.tool;

import com.github.pagehelper.util.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期工具
 * 
 * @author K
 *
 */
public class DateUtill {
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public final static String DATE_PATTERN = "yyyMMdd";

	/**
	 * 日期格式化
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			return sdf.format(date);
		}
		return null;
	}

	/**
	 * 字符串转日期
	 * 
	 * @param strDate
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToDate(String strDate, String pattern) throws ParseException {
		if (StringUtil.isNotEmpty(strDate)) {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			return sdf.parse(strDate);
		}
		return null;

	}

	public static String getnowtime() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
		return sdf.format(new Date());
	}
	
	public static String dateRoll(String minute) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(Integer.parseInt(minute));
        return dateTimeFormatter.format(localDateTime);
    }
}
